package com.authenticateCenter.controller;

import com.authenticateCenter.assembler.AuthorityModuleAssembler;
import com.authenticateCenter.domain.dto.AuthorityModuleDto;
import com.authenticateCenter.domain.entity.AuthorityModule;
import com.authenticateCenter.domain.vo.AuthorityModuleVo;
import com.authenticateCenter.service.IAuthorityModuleService;
import com.authenticateCenter.service.IAuthorityService;
import com.authenticateCenter.service.IRoleAuthorityService;
import com.common.mybaits.CustomPage;
import com.common.result.ResponseResultAdvice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

import static com.authenticateCenter.factory.AuthorityModuleFactory.AUTHORITY_MODULE_CONVERT;

/**
 * @author 大忽悠
 * @create 2022/9/15 11:09
 */
@Validated
@RestController
@RequestMapping("/authorityModule")
@RequiredArgsConstructor
@Api("资源模块")
@ResponseResultAdvice
public class AuthorityModuleController {
    private final IAuthorityModuleService iAuthorityModuleService;
    private final IAuthorityService iAuthorityService;
    private final IRoleAuthorityService iRoleAuthorityService;
    private final AuthorityModuleAssembler authorityModuleAssembler;

    @ApiOperation("新增资源模块")
    @PostMapping("/add")
    public Integer addAuthorityModule(@RequestBody@ApiParam("parentId不传表示当前资源模块为顶层模块") AuthorityModuleDto authorityModuleDto){
        AuthorityModule authorityModule = AUTHORITY_MODULE_CONVERT.toAuthorityModule(authorityModuleDto);
        authorityModule.insertCheck(iAuthorityModuleService);
        return authorityModule.getId();
    }

    @ApiOperation("删除资源模块")
    @DeleteMapping("/delete")
    public void deleteAuthorityModule(@RequestParam("id")Integer authorityModuleId) throws ExecutionException, InterruptedException {
        iAuthorityModuleService.removeAuthorityModule(authorityModuleId,iAuthorityService,iRoleAuthorityService);
    }

    @ApiOperation("获取所有资源模块信息")
    @DeleteMapping("/ListOfAuthorityModule")
    public List<AuthorityModuleVo> getAllAuthorityModuleList(CustomPage customPage){
        return authorityModuleAssembler.assemblers(customPage,null);
    }
}
